<?php
	class page
	{
		private $db;
		private $user;
		private $html;
		public	$title;
		public	$request;
		public	$params;
		public 	$GZIP;
		private	$lang_dir;
		private $lang_data;

		public function __construct()
		{
			$this->db = & $GLOBALS['db'];
			//$this->cache = & $GLOBALS['cache'];
			$this->user = & $GLOBALS['user'];
			$this->request = array();
			$this->params = new stdClass();
			$this->title = NULL;
			$this->html = NULL;
			$this->GZIP = TRUE;
			$this->lang_dir = INCPATH.'lang_'.LANG;
			$this->lang_data = array();
			$this->params->user = FALSE;
		}
		public function return_html()
		{
			$this->params->layout = 1;
			$this->parse_input();
			//if(FALSE == $this->user->is_logged)
			//{
			//	$this->user->try_autologin()
			//}
			//$this->load_langfile('global.php');
			

			$html = $this->load_page();
			$this->html .= $this->load_header();
			$this->html .= $html;
			$this->html .= $this->load_footer();
			//if(TRUE == $this->GZIP)
			//{
			//	$res = ob_gzhandler($this->html, 5);
			//	if( FALSE !== $res )
			//	{	
			//		header('Content-Encoding: gzip');
			//		return $res;
			//	}
			//}
			//$this->html = $this->request[0];
			return $this->html;	
		}
		private function load_header()
		{
			$html = '';
			if(0 == strcmp($this->request[0],'profile'))
			{
				require_once(PAGES.'profile_html_header.php');
			}
			else
			{
				require_once(PAGES.'html_header.php');
			}
			return $html;
		}
		private function load_footer()
		{
			$html = '';
			require_once(PAGES.'html_footer.php');
			return $html;
		}
		private function parse_input()
		{
			//$this->params->user = FALSE;
			if(FALSE == isset($_GET['request']) || empty($_GET['request']))
			{
				$this->request[] = $this->user->is_logged?'home':'profile';
				return;
			}
			$request = $_GET['request'];
			$pos = strpos(DOMAIN,'/');
			if(FALSE !== $pos)
			{
				$tmp = strlen(substr(DOMAIN,$pos));
				$request = substr($request,$tmp);
				$request = ltrim($request,'/');
			}
			$tmp = explode('/',$request);
			$tmp = array_delempty($tmp,FALSE);
			foreach($tmp as $i=>$one)
			{
				if($one == 'm')
				{
					unset($tmp[$i]);
					continue;
				}
				if(FALSE == preg_match('/^([a-z0-9\-_]+)$/iu',$one))
				{
					unset($tmp[$i]);
				}
			}
			if(0 == count($tmp))
			{
				$this->request[] = $this->user->is_logged?'home':'profile';
				return ;
			}
			$tmp = array_values($tmp);
			$first = $tmp[0];
			if(file_exists(PAGES.'pg_'.$first.'.php'))
			{
				$this->request[] = $first;
			}
			else 
			{
				$this->request[] = $this->user->is_logged ? 'home' : 'profile';
				return;
			}
			unset($tmp[0]);
			foreach($tmp as $one)
			{
				$this->params->argv[] = $one;
			}
			//$this->params->user = $this->user->is_logged ? $this->user->id : FALSE;
			if(0 == count($this->request))
			{
				$this->request[] = $this->user->is_logged ? 'home' : 'profile';
			}
		}
		private function load_page()
		{
			$file = 'pg_'.$this->request[0].'.php';
			$html = '';
			require_once(PAGES.$file);
			return $html;
		}
		
	}
?>